import 'package:flutter/material.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/bookGridWithCheckBox.dart';

class SearchBooksPage extends StatelessWidget {
  SearchBooksPage({Key? key}) : super(key: key);
  final BooksController _booksController = Get.find();
  static const String routeName = '/search';

  @override
  Widget build(BuildContext context) {
    Log.debug('build common SearchBooksPage');
    TextEditingController searchTextController = new TextEditingController();

    return Scaffold(
      appBar: AppBar(
          title: Container(
        width: double.infinity,
        height: 40,
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(5)),
        child: Center(
          child: TextFormField(
            controller: searchTextController,
            onFieldSubmitted: (query) {
              if (query.trim() != '') {
                BookSearchDto dto = new BookSearchDto();
                dto.query = query;
                _booksController.searchedBookItems.clear();
                _booksController.fetchSearchedBookItems(0, 0, dto, '');
              }
            },
            decoration: InputDecoration(
                prefixIcon: Icon(Icons.search),
                suffixIcon: IconButton(
                  icon: Icon(Icons.clear),
                  onPressed: () {
                    searchTextController.text = '';
                  },
                ),
                hintText: 'Search ...'.tr,
                border: InputBorder.none),
          ),
        ),
      )),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Center(
          child: _booksController.obx(
            (data) => BookGridWithCheckBox(
              books: _booksController.searchedBookItems,
              readingType: NanhaiBookReadingTypes.search,
            ),
            onEmpty: Text('Nothing found!'.tr),
            onLoading: Container()
          ),
        ),
      ),
    );
  }
}
